package com.oxbix.spanlogistics.share.sina;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.kye.express.R;
import com.kye.express.view.ToastManager;
import com.oxbix.spanlogistics.share.Contants;
import com.oxbix.spanlogistics.share.ShareBean;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;


public class SINAEntryActivity extends AppCompatActivity implements WbShareCallback {

    private SsoHandler mSsoHandler;
    private WbShareHandler mWbShareHandler;
    private Bitmap mShareBitmap;
    private ShareBean mShareBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sinaentry);
        initWeiBo();
        loadData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recyleBitmap();
    }

    protected void loadData() {
        mShareBean = (ShareBean) getIntent().getSerializableExtra(ShareBean.class.getName());
        doShare();

    }

    private void initWeiBo() {
        WbSdk.install(this, new AuthInfo(this, Contants.SINA_APP_KEY, Contants.REDIRECT_URL, Contants.SCOPE));//创建微博API接口类对象
        mWbShareHandler = new WbShareHandler(this);
        mWbShareHandler.registerApp();
    }


    public void recyleBitmap() {
        if (mShareBitmap != null) {
            mShareBitmap.recycle();
            mShareBitmap = null;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mWbShareHandler.doResultIntent(intent, this);
    }


    @Override
    public void onWbShareSuccess() {
        ToastManager.show(SINAEntryActivity.this, "新浪微博 分享成功！", Toast.LENGTH_SHORT);
        recyleBitmap();
        finish();
    }

    @Override
    public void onWbShareCancel() {
        ToastManager.show(SINAEntryActivity.this, "新浪微博 分享取消！", Toast.LENGTH_SHORT);
        recyleBitmap();
        finish();

    }

    @Override
    public void onWbShareFail() {
        ToastManager.show(SINAEntryActivity.this, "新浪微博 分享失败！", Toast.LENGTH_SHORT);
        recyleBitmap();
        finish();
    }


    public void doShare() {
        sendMultiMessage(true, true);
    }


    /**
     * 第三方应用发送请求消息到微博，唤起微博分享界面。
     */
    private void sendMultiMessage(boolean hasText, boolean hasImage) {

        WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
        if (hasText) {
            weiboMessage.textObject = getTextObj();
        }
        if (hasImage) {
            weiboMessage.imageObject = getImageObj();
        }
        mWbShareHandler.shareMessage(weiboMessage, false);
    }

    /**
     * 创建文本消息对象。
     *
     * @return 文本消息对象。
     */
    private TextObject getTextObj() {
        TextObject textObject = new TextObject();
        textObject.text = mShareBean.getSummary();
        textObject.title = mShareBean.getTitle();
        textObject.actionUrl = mShareBean.getLinkUrl();
        return textObject;
    }


    /**
     * 创建图片消息对象。
     *
     * @return 图片消息对象。
     */
    private ImageObject getImageObj() {
        ImageObject imageObject = new ImageObject();
        try {
            if (mShareBean.getPath() != null) {
                mShareBitmap = BitmapFactory.decodeFile(mShareBean.getPath());
                imageObject.setImageObject(mShareBitmap);
            } else {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                imageObject.setImageObject(bitmap);
            }
        } catch (Exception e) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            imageObject.setImageObject(bitmap);
        }
        return imageObject;
    }

}
